package com.ita.tba.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.DoubleAdder;

import org.omg.CORBA.ObjectHolder;

import com.ita.tba.dao.CartDao;
import com.ita.tba.dao.DishDao;
import com.ita.tba.dao.OrderListDao;
import com.ita.tba.dao.UserDao;
import com.ita.tba.dao.impl.CartDaoImpl;
import com.ita.tba.dao.impl.DishDaoImpl;
import com.ita.tba.dao.impl.OrderListDaoImpl;
import com.ita.tba.dao.impl.UserDaoImpl;
import com.ita.tba.po.Cart;
import com.ita.tba.po.Dish;
import com.ita.tba.po.DishCache;
import com.ita.tba.po.OrderList;
import com.ita.tba.po.User;
import com.ita.tba.service.Controller;

public class ControllerImpl implements Controller{
	private CartDao cartDao = new CartDaoImpl();
	private DishDao dishDao = new DishDaoImpl();
	private OrderListDao orderListDao = new OrderListDaoImpl();
	private UserDao userDao = new UserDaoImpl();
	
	@Override
	public boolean isEmailExist(String email) {
		String findEmail = userDao.findEmail(email);
		
		if (findEmail != null) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public void addUser(User user) {
		int m = userDao.insertUser(user);
	}

	@Override
	public boolean isPasswordCorrect(String email, String password) {
		String passwordStr = userDao.findPassword(email);
		
		if (password.equals(passwordStr)) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public boolean addToCart(int dishId, int uid) {
		Cart cart = cartDao.findCart(uid);
		Map<Integer, Dish> dishCache = new HashMap<Integer, Dish>();
		dishCache = DishCache.dishCache;
		
		if(!cart.getDidNum().isEmpty()) {
			Map<Integer, Integer> didNumTemp = cart.getDidNum();
			double totalPrice = cart.getTotalPrice();
			boolean isDidExist = false;
			for (Integer did : didNumTemp.keySet()) {
				if (dishId == did) {
					isDidExist = true;
					int num = didNumTemp.get(did);
					num++;
					didNumTemp.replace(did, num);
				}
			}
			if (!isDidExist) {
				didNumTemp.put(dishId, 1);
			}
			double price = dishCache.get(dishId).getPrice();
			totalPrice += price;	
			
			int m = cartDao.updateCart(uid, didNumTemp, totalPrice);
			if (m != 0) {
				return true;
			}
		}
		else {
			Map<Integer, Integer> didNum = new HashMap<Integer, Integer>();
			didNum.put(dishId, 1);
			
			double price = dishCache.get(dishId).getPrice();
			double totalPrice = price * didNum.get(dishId);
			
			Cart newCart = new Cart(didNum, uid, totalPrice);
			int m = cartDao.addCart(newCart);
			if (m != 0) {
				return true;
			}
		}
		
		return false;
	}

	@Override
	public int getUid(String email) {
		
		return userDao.selectUid(email);
	}

	@Override
	public Cart getThisCart(int uid) {		
		return cartDao.findCart(uid);
	}

	@Override
	public int getDid(String dname) {
		
		return dishDao.selectDid(dname);
	}

	@Override
	public boolean removeDish(int dishId, int uid) {
		Cart cart = cartDao.findCart(uid);
		Map<Integer, Dish> dishCache = new HashMap<Integer, Dish>();
		dishCache = DishCache.dishCache;
		
		Map<Integer, Integer> didNumTemp = cart.getDidNum();
		double totalPrice = cart.getTotalPrice();
		
		int numTemp = -1;
		for (Integer did : didNumTemp.keySet()) {
			if (dishId == did) {	
				numTemp = didNumTemp.get(did);
				numTemp--;
				
			}
		}
		
		if (numTemp == 0) {
			didNumTemp.remove(dishId);
			if (didNumTemp.isEmpty()) {
				clearCart(uid);
			}
		}else {
			didNumTemp.replace(dishId, numTemp);
		}
		
		double price = dishCache.get(dishId).getPrice();
		totalPrice -= price;	
		
		int m = cartDao.updateCart(uid, didNumTemp, totalPrice);
		if (m != 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean addOrderList(OrderList orderList) {
		int m = orderListDao.insertOrderList(orderList);
		if (m != 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean clearCart(int uid) {
		int m = cartDao.deleteCart(uid);
		if (m != 0) {
			return true;
		}
		return false;
	}

	@Override
	public List<OrderList> getOrderList(int uid) {
		
		return orderListDao.selectOrderList(uid);
	}

}
